#!/bin/bash
conf() {
	if [ ! -n "$2" ];then
		return 1
	fi
	if [ "$1" == "-l" ];then
		echo "$2" | sed -n 'p;n'
	elif [ "$1" == "-r" ];then
		if [ ! -n "$3" ];then
			return 1
		fi
		line="$(echo "$2" | sed -n 'p;n' | grep -Fxn "$3" | sed -n '1p' | cut -d ':' -f 1)"
		if [ -n "$line" ];then
			echo "$2" | sed -n "$[$line*2]p" | sed 's/\\n/\'$'\n''/g' | sed 's/\\ /\\/g'
		else
			return 1
		fi
	elif [ "$1" == "-m" ];then
		if [ ! -n "$4" ];then
			return 1
		fi
		line="$(echo "$2" | sed -n 'p;n' | grep -Fxn "$3" | sed -n '1p' | cut -d ':' -f 1)"
		if [ -n "$line" ];then
			line="$[$line*2]"
			echo -n "$(echo "$2" | sed -n "1,$[$line-1]p")"$'\n'"$(echo "$4"$'\n' | sed 's/\\/\\ /g' | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g' | sed '$s/..$//')"$'\n'"$(echo "$2" | sed -n "$[$line+1],$(echo "$2" | wc -l)p")"
		else
			echo "$2"$'\n'"$3"$'\n'"$4"
		fi
	else
		return 1
	fi
}

operation="$1"
file="$2"
shift 2
conf "$operation" "$(cat "$file")" "$@"
